<?php
/**
 * 广告项目控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_project extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('ads_project_model');
		
		$this->loadClass("Mcache");

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/project/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return max
	 */
	function index()
	{
		$this->getProjects();
	}

	/**
	 * 添加项目
	 * @return max
	 */
	function addProject(){
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("project.html");
	}

	/**
	 * 编辑项目
	 * @return max
	 */
	function EditProject(){

		$id = intval($this->in_var['item']);
		
		$project = $this->ads_project_model->getAdsProjectById($id);

		$this->tpl->assign("project",$project);
		$this->tpl->display("project.html");
	}

	/**
	 * 获取多个项目
	 * @return max
	 */
	function getProjects(){
		$page = max($this->in_var['page'],1);
		
		$this->result['projects'] = $this->ads_project_model->getAdsProjectsByKeywords('',$page,5);
		
		$page_info = $this->ads_project_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);
		
		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("projects.html");

	}
	/**
	 * 根据条件查询项目
	 * @return max
	 */
	function searchProjects(){

		$page     = max($this->in_var['page'],1);
		$keywords = $this->in_var['keywords'];
		
		$this->result['projects'] = $this->ads_project_model->getAdsProjectsByKeywords($keywords,$page,5);
		$page_info = $this->ads_project_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);
		
		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("projects.html");
	}

	/**
	 * 保存项目
	 * @return max
	 */
	function saveProject(){

		$flag = false;
		$project = $this->in_var['project'];

//		if(empty($project['p_name'])||empty($project['p_email'])||empty($project['p_phone'])||empty($project['p_link']))
//		{
//			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['error'],'status'=>false)));
//		}

		if(!empty($project['p_id']))
		{
			$flag = $this->ads_project_model->changeAdsProjectById($project);
			$this->Mcache->clearcache("ads",'projects');
		}
		else
		{
			$flag = $this->ads_project_model->addAdsProject($project);
		}

		exit(json_encode($flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['save_success'],'status'=>true):$this->result['msg'] = array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['save_error'],'status'=>false)));
	}

	/**
	 * 删除多个项目
	 * @return max
	 */
	function delProjects(){
		
		$ids = $this->in_var['item'];

		$id_ = explode(',',$ids);
		
		try{
			
			foreach($id_ as $id)
			{
				$this->ads_project_model->delAdsProjectById($id);
			}
			
			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['del_success'],'status'=>true)));
		}
		catch(Exception $e)
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['project']['del_error'],'status'=>false)));
		}
	}

}